window.onload=function (){
 
	$("#save_menu_header").after('<input type="button"  id="proSort" class="button button-primary alignright" value="自动排序" >');  
     
 // $("#save_menu_header").before('<input type="button"  id="proSort" class="button button-primary alignright" value="产品分类排序" >');  
  $("#proSort").css("background-color","#111");
  $("#proSort").css("margin-inline-end","8px");

  $("#proSort").click(function(){
		setpro();		
		$("#save_menu_header").trigger("click");			
  }) 
    
	
 function setpro(){
 function getdbidBypostid(postid){var dbid="0";$("#menu-to-edit .menu-item-data-object-id").each(function(index){if($(this).attr("value")==postid){dbid=$(this).siblings(".menu-item-data-db-id").val()}});return dbid}var Postid=new Array();function setMenu(){$("#tabs-panel-category-all .menu-item-checkbox").each(function(index){Postid.push($(this).attr("value"))});for(var i=0;i<Postid.length;i++){$("#category-"+Postid[i]+" .children .menu-item-checkbox").each(function(index){var children=$(this).attr("value");var dbid=getdbidBypostid(Postid[i]);console.log(Postid[i]+"[dbid:"+dbid+"]"+"的孩子(postid)："+children);$("#menu-to-edit .menu-item-data-object-id").each(function(index){if($(this).attr("value")==children){$(this).siblings(".menu-item-data-parent-id").val(dbid)}})})}console.log("站点一共有"+Postid.length+"个产品分类，添加到菜单的分类层级设置成功！保存即可！")}setMenu();
 }
 function setnews(){
 function getdbidBypostid(postid){var dbid="0";$("#menu-to-edit .menu-item-data-object-id").each(function(index){if($(this).attr("value")==postid){dbid=$(this).siblings(".menu-item-data-db-id").val()}});return dbid}var Postid=new Array();function setNewsMenu(){$("#tabs-panel-news_catalog-all .menu-item-checkbox").each(function(index){Postid.push($(this).attr("value"))});for(var i=0;i<Postid.length;i++){$("#category-"+Postid[i]+" .children .menu-item-checkbox").each(function(index){var children=$(this).attr("value");var dbid=getdbidBypostid(Postid[i]);console.log(Postid[i]+"[dbid:"+dbid+"]"+"的孩子(postid)："+children);$("#menu-to-edit .menu-item-data-object-id").each(function(index){if($(this).attr("value")==children){$(this).siblings(".menu-item-data-parent-id").val(dbid)}})})}console.log("站点一共有"+Postid.length+"个新闻分类，添加到菜单的分类层级设置成功！保存即可！")}setNewsMenu();
 }

 function setpage(){
 function getdbidBypostid(postid){var dbid="0";$("#menu-to-edit .menu-item-data-object-id").each(function(index){if($(this).attr("value")==postid){dbid=$(this).siblings(".menu-item-data-db-id").val()}});return dbid}var Postid=new Array();function setPageMenu(){$("#page-all .menu-item-checkbox").each(function(index){Postid.push($(this).attr("value"))});for(var i=0;i<Postid.length;i++){$("#category-"+Postid[i]+" .children .menu-item-checkbox").each(function(index){var children=$(this).attr("value");var dbid=getdbidBypostid(Postid[i]);console.log(Postid[i]+"[dbid:"+dbid+"]"+"的孩子(postid)："+children);$("#menu-to-edit .menu-item-data-object-id").each(function(index){if($(this).attr("value")==children){$(this).siblings(".menu-item-data-parent-id").val(dbid)}})})}console.log("站点一共有"+Postid.length+"个页面分类，添加到菜单的分类层级设置成功！保存即可！")}setPageMenu();
 } 
 
 /*****************复制分类顺序******************************/
$("#save_menu_header").after('<input type="button"  id="copyCats" class="button button-primary alignright" value="复制" ><input type="button"  id="setCats" class="button button-primary alignright" value="粘贴" >');  

$("#copyCats").click(function(){	
	copyCats();
})
$("#setCats").click(function(){
	 var a = [];
	 var str= window.localStorage.getItem("copyCats"); 
	 a=str.split(',');
	setCats(a);//调整位置	
	setpro();	//设置父级关系
	$("#save_menu_header").trigger("click");	//保存	
})
function copyCats(){
var a=[];
$("#menu-to-edit .menu-item-data-object-id ").each(function(){	
	a.push($(this).val());	
})
if(!window.localStorage){
            alert("浏览器支持localstorage");
            return false;
        }else{
           
            window.localStorage["copyCats"]=a; 
			alert("复制成功");
        }
}
function setCats(a){
	var b=[];
$("#menu-to-edit li").each(function(){	
	var c={};
	c[1]=$(this).find(".menu-item-data-object-id").val();
	c[2]=$(this).html();
	b.push(c);	
	$(this).remove()
})
for(var i= 0;i<a.length;i++){
for(var j= 0;j<b.length;j++){
if(a[i]==b[j][1]){
	b[j][0]=i;}	}}
b.sort(sortNumber);
function sortNumber(a,b){return a[0] - b[0];}
for(var j = 0;j<b.length;j++){
 	$("#menu-to-edit").append('<li class="menu-item menu-item-depth-0 menu-item-category menu-item-edit-inactive">'+b[j][2]+'</li>')
}
console.log(b);	
}

 /*****************删除菜单li******************************/
 $("#menu-to-edit li").each(function(){
	 
	 $(this).find(".item-controls").append('<span class="item-delete" style="float: left;color: red;">移除</span>')
	 
 })



$(".item-delete").click(function(){
	$(this).parents(".menu-item").remove();
})











}

